package br.srv.full.virtual.entities.resume;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import br.srv.full.virtual.entities.auth.Login;
import br.srv.full.virtual.entities.auth.Role;
import br.srv.full.virtual.entities.auth.User;

/**
 * @author Delfino
 * 
 */
public class UserLogin implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4787202540442076474L;

	private String checkPassword;

	private Boolean logged = false;
	private User user;
	private Login login;

	/**
	 * @param p_user
	 * @param p_login
	 */
	public UserLogin(User p_user, Login p_login) {
		setUser(p_user);
		setLogin(p_login);
	}

	/**
	 * 
	 */
	public UserLogin() {
	}

	/**
	 * Cria um UserLogin com base no Login e usuário representado por ele.
	 * 
	 * @param p_login
	 * 
	 */
	public UserLogin(Login p_login) {
		setLogin(p_login);
		setUser(p_login.getUser());

	}

	/**
	 * @return
	 */
	public Boolean getLogged() {
		return logged;
	}

	/**
	 * @param _logged
	 */
	public void setLogged(Boolean _logged) {
		logged = _logged;
	}

	/**
	 * @return
	 */
	public String getUserLogin() {
		return getLogin().getLoginName();
		// return userLogin;
	}

	/**
	 * No sistema FullService Virtual o Login é feito com o E-mail do Usuário.
	 * 
	 * @param _login
	 */
	public void setUserLogin(String _login) {
		getLogin().setLoginName(_login);

		// userLogin = _login;
	}

	/**
	 * @return
	 */
	public String getName() {
		return getUser().getName();
		// return name;
	}

	/**
	 * @param name
	 */
	public void setName(String name) {
		getUser().setName(name);
		// this.name = name;
	}

	/**
	 * @return
	 */
	public String getPassword() {
		return getLogin().getPassword();
		// return password;
	}

	/**
	 * @param password
	 */
	public void setPassword(String password) {
		getLogin().setPassword(password);
		// this.password = password;
	}

	/**
	 * @return
	 */
	public String getAka() {
		return getUser().getAka();
		// return aka;
	}

	/**
	 * @param aka
	 */
	public void setAka(String aka) {
		getUser().setAka(aka);
		// this.aka = aka;
	}

	/**
	 * @return
	 */
	public String getEmail() {
		return getUser().getEmail();
		// return email;
	}

	/**
	 * @param _email
	 */
	public void setEmail(String _email) {
		String email = _email != null ? _email.toLowerCase() : _email;
		getUser().setEmail(email);
	}

	/**
	 * @return
	 */
	public String getCheckPassword() {
		return checkPassword;
	}

	/**
	 * @param checkPassword
	 */
	public void setCheckPassword(String checkPassword) {
		this.checkPassword = checkPassword;
	}

	/**
	 * @return
	 */
	public Date getValidate() {
		return getLogin().getValidate();
		// return validate;
	}

	/**
	 * @param validate
	 */
	public void setValidate(Date validate) {
		getLogin().setValidate(validate);
		// this.validate = validate;
	}

	/**
	 * @param _id
	 */
	public void setRegistrationId(String _id) {
		getUser().setRegistrationId(_id);
		// registrationId = _id;
	}

	/**
	 * @return
	 */
	public Date getCreateDate() {
		return getLogin().getCreateDate();
		// return createDate;
	}

	/**
	 * @param createDate
	 */
	public void setCreateDate(Date createDate) {
		getLogin().setCreateDate(createDate);
		// this.createDate = createDate;
	}

	/**
	 * @return
	 */
	public Date getRegistrationDate() {
		return getUser().getRegistrationDate();
		// return registrationDate;
	}

	/**
	 * @param registrationDate
	 */
	public void setRegistrationDate(Date registrationDate) {
		getUser().setRegistrationDate(registrationDate);
		// this.registrationDate = registrationDate;
	}

	/**
	 * @return
	 */
	public Integer getUserId() {
		return getUser().getId();
		// return userId;
	}

	/**
	 * @param _userId
	 */
	public void setUserId(Integer _userId) {
		getUser().setId(_userId);
		// this.userId = _userId;
		// setLoginId(_userId);
	}

	/**
	 * @return
	 */
	public String getRegistrationId() {
		return getUser().getRegistrationId();
		// return registrationId;
	}

	/**
	 * @return
	 */
	public Integer getLoginId() {
		return getLogin().getId();
		// return loginId;
	}

	/**
	 * @param _loginId
	 */
	public void setLoginId(Integer _loginId) {
		getLogin().setId(_loginId);
		// this.loginId = _loginId;
		// setUserId(_loginId);
	}

	/**
	 * @return
	 */
	public Integer getAccessCount() {
		return getLogin().getAccessCount();
		// return accessCount;
	}

	/**
	 * @param _acessCount
	 */
	public void setAccessCount(Integer _acessCount) {
		getLogin().setAccessCount(_acessCount);
		// accessCount = _acessCount;
	}

	/**
	 * @return
	 */
	public boolean isFirstAccess() {
		boolean l_firstAccess = getAccessCount() == null || getAccessCount() == 0;
		return l_firstAccess;
	}

	/**
	 * @return the roles
	 */
	public List<Role> getRoles() {
		return getLogin().getRoles();
		// return roles;
	}

	/**
	 * @param p_roles
	 *            the roles to set
	 */
	public void setRoles(List<Role> p_roles) {
		getLogin().setRoles(p_roles);
		// roles = p_roles;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "User Name: " + getName() + " Login: " + getUserLogin();
	}

	/**
	 * @return the siteUseControl
	 */
	/*
	 * public List<SiteUseControl> getSiteUseControl() { return siteUseControl; }
	 * 
	 *//**
		 * @param p_siteUseControl
		 *            the siteUseControl to set
		 */
	/*
	 * public void setSiteUseControl(List<SiteUseControl> p_siteUseControl) {
	 * siteUseControl = p_siteUseControl; }
	 */

	public User getUser() {
		if (user == null) {
			user = new User();
		}

		return user;
	}

	public Login getLogin() {
		if (login == null) {
			login = new Login();
		}

		return login;
	}

	/**
	 * @param p_newLogin
	 */
	public void setLogin(Login p_login) {
		login = p_login;
	}

	/**
	 * @param p_user
	 */
	public void setUser(User p_user) {
		user = p_user;
	}

	/**
	 * @return
	 */
	public Boolean isLogged() {
		if (logged == null)
			logged = false;
		return logged;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object p_obj) {
		boolean l_equals = p_obj != null;
		l_equals &= p_obj instanceof UserLogin;
		if (l_equals) {
			
			UserLogin l_userLogin = (UserLogin) p_obj;
			Login l_login2 = l_userLogin.getLogin();
			Login l_login = getLogin();

			l_equals &= l_login != null && l_login2 != null;
			if (l_equals) {
				l_equals &= l_login.equals(l_login2);
			}
		}
		return l_equals;
	}
}
